﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using YunCMS.BLL;
using YunCMS.Model;
using YunCMS.Common;

namespace YunCMS.Controls.Page
{
    public abstract class ModulePage<T> : AuthPage 
        where T : YCModule, new()
    {
        public ModulePage()
        { 
            this.Load +=new EventHandler(ModulePage_Load);
        }

        private void ModulePage_Load(object sender, EventArgs e)
        {
           
        }

        protected override string Module
        {
            get
            {
                return "ContentManage";
            }
        }


        protected abstract ModuleBLL<T> BLL
        {
            get;
        }

        protected virtual void HandleAction(LinkButton saveBt)
        {
            string action = Request.QueryString["action"];
            int id;
            if (!string.IsNullOrEmpty(action) && action == "edit")
            {
                string _id = Request.QueryString["id"];

                if (!int.TryParse(_id, out id))
                    id = 0;

                ViewState["ID"] = id;
                BindControls(id);

                saveBt.Text = "更新";
            }
            else
            {
                saveBt.Text = "新增";
            }
        }

        protected virtual void BindControls(int id)
        {
            if (BLL.IsExist(id))
            {
                T model = BLL.GetModule(id);
                FillControls(model);
            }
            else
            {
                JScript.Alert("不存在该条记录", this);
            }
        }

        protected virtual void FillControls(T model)
        {
        }

        protected virtual void FillModel(T model)
        {
        }

        protected virtual void Save(string url)
        {
            T model;
            int i = 0, id;

            string action = Request.QueryString["action"];
            if (!string.IsNullOrEmpty(action) && action == "edit")
            {
                if (ViewState["ID"] != null)
                {
                    id = System.Convert.ToInt32(ViewState["ID"]);
                    model = BLL.GetModule(id);
                    FillModel(model);
                    i = BLL.Update(model);
                }
            }
            else
            {
                model = new T();
                FillModel(model);
                i = BLL.Add(model);
            }

            if (i > 0)
            {
                Response.Redirect(url);
            }
        }


        protected virtual void TreeBind(DropDownList ddl, string moduleType)
        {
            YCCategoryBLL bll = new YCCategoryBLL();
            IList<YCCategory> list = bll.GetList(moduleType);

            ddl.Items.Clear();
            ddl.Items.Add(new ListItem("无父级分类", "0"));
            foreach (YCCategory item in list)
            {
                string id = item.ID.ToString();
                int classLayer = 0;
                if (item.NodeLevel.HasValue)
                {
                    classLayer = item.NodeLevel.Value;
                }
                string title = item.Title.ToString().Trim();

                if (classLayer == 1)
                {
                    ddl.Items.Add(new ListItem(title, id));
                }
                else
                {
                    title = "├ " + title;
                    title = StringUtil.NString(classLayer - 1, "　") + title;
                    ddl.Items.Add(new ListItem(title, id));
                }
            }
        }         
    }
}
